﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Imgur.Net
{

    /// <summary>
    /// Exception denoting an error in the API response resulting from a web service call.
    /// </summary>
    [Serializable]
    public class ResponseException : Exception
    {
        /// <summary>
        /// Creates an instance of this exception represeting the given status code and success level.
        /// </summary>
        /// <param name="status">The status code from the result data.</param>
        /// <param name="success">The success code from the result data.</param>
        public ResponseException(int status, bool success)
        {
            Status = status;
            Success = success;
        }

        /// <summary>
        /// Creates an instance of this exception represeting the given status code and success level.
        /// </summary>
        /// <param name="message">Description of the error.</param>
        /// <param name="status">The status code from the result data.</param>
        /// <param name="success">The success code from the result data.</param>
        public ResponseException(string message, int status, bool success) : base(message)
        {
            Status = status;
            Success = success;
        }

        /// <summary>
        /// Creates an instance of this exception represeting the given status code and success level. This wraps an inner exception.
        /// </summary>
        /// <param name="message">Description of the error.</param>
        /// <param name="innerException"></param>
        /// <param name="status">The status code from the result data.</param>
        /// <param name="success">The success code from the result data.</param>
        public ResponseException(string message, Exception innerException, int status, bool success) : base(message, innerException)
        {
            Status = status;
            Success = success;
        }

        protected ResponseException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            if (info != null)
            {
                this.Status = info.GetInt32("Status");
                this.Success = info.GetBoolean("Success");
            }
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            if (info != null)
            {
                info.AddValue("Status", this.Status);
                info.AddValue("Success", this.Success);
            }
        }

        /// <summary>
        /// Status code from the response.
        /// </summary>
        public int Status { get; private set; }

        /// <summary>
        /// Success flag. True for success or False for failure.
        /// </summary>
        public bool Success { get; private set; }
    }
}
